<?php
namespace App\Models;

use App\Constants\TagConstant;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Topic extends Model
{
    use SoftDeletes;

    use HasDateTimeFormatter;

    protected $table = 'topics';

    protected $primaryKey = 'id';

    protected $fillable = [
        'member_id', // 用户ID
        'content', // 内容
        'files', // 文件
        'relay_count', // 转发数
        'fav_count', // 点赞数
        'is_hot', // 推荐：0、否；1、是
        'status', // 状态：0、未审核；1、已通过；2、未通过
        'tag_id', // 标签id
        'mini_type', // 小程序类型
        'title', // 标题
    ];

    protected $dates=['delete_at'];

    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    public function topicComment(){
        return $this->hasMany(TopicComment::class,'topic_id','id');
    }

    public function topicTag()
    {
        return $this->hasOne(Tag::class, 'id', 'tag_id')->where('type', '=', TagConstant::TAG_BY_TOPIC);
    }

    public function topicTheme(){
        return $this->belongsToMany(Theme::class, 'topics_theme', 'topic_id', 'theme_id');
    }
}
